/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import BUS.CTPhieuDatPhong_BUS;
import BUS.CTPhieuThuePhong_BUS;
import BUS.DoanhThuTheoLoaiPhong_BUS;
import BUS.KhachHang_BUS;
import BUS.PhieuDatPhong_BUS;
import BUS.PhieuThuePhong_BUS;
import BUS.Phong_BUS;
import DTO.CTPhieuThuePhong_DTO;
import DTO.KhachHang_DTO;
import DTO.PhieuDatPhong_DTO;
import DTO.PhieuThuePhong_DTO;
import DTO.Phong_DTO;
import com.lowagie.text.pdf.ArabicLigaturizer;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Lenh Ho Xung
 */
public final class PhieuThuePhong extends javax.swing.JFrame {

    DefaultTableModel modelKhachDatPhong;
    DefaultTableModel modelPhongDeThue;
    DefaultTableModel modelPhongThue;

    /**
     * Creates new form PhieuThuePhong
     */
    public PhieuThuePhong() {
        initComponents();
        showPhongDeThue();
        showTablePhongThue();
        huyPhieuDatQuaHan();

    }

    public void showPhongDeThue() {
        try {
            modelPhongDeThue = Phong_BUS.layDanhSachPhongDeThue();
        } catch (Exception ex) {
            Logger.getLogger(PhieuThuePhong.class.getName()).log(Level.SEVERE, null, ex);
        }
        tbPhongDeThue.setModel(modelPhongDeThue);
    }

    public void showTablePhongThue() {
        Vector column = new Vector();
        column.add("Mã Phòng");
        column.add("Tên Loại Phòng");
        column.add("Đơn Giá");
        modelPhongThue = new DefaultTableModel(column, 0);
        tbPhongThue.setModel(modelPhongThue);
    }

    public void huyPhieuDatQuaHan() {
        SimpleDateFormat format = new SimpleDateFormat("MM-dd-yyyy");
        Date dtoday = new Date(System.currentTimeMillis() - 100742784);
        String today = format.format(dtoday);
        try {
            ArrayList<PhieuDatPhong_DTO> ds = PhieuDatPhong_BUS.layDSMaPhieuDatQuaHan(today);
            for (int i = 0; i < ds.size(); i++) {
                String maPhieuDat = ds.get(i).getMaPhieuDat().toString();
                ArrayList<Phong_DTO> dsmaPhong = new ArrayList<>();
                dsmaPhong = PhieuDatPhong_BUS.layDSMaPhongDat(maPhieuDat);
                for (int j = 0; j < dsmaPhong.size(); j++) {
                    Phong_BUS.capnhattinhtrangDat(dsmaPhong.get(j).getMaPhong().toString(), "Trống");
                }
                CTPhieuDatPhong_BUS.Delete(maPhieuDat);
                PhieuDatPhong_BUS.Delete(maPhieuDat);
            }

        } catch (Exception ex) {
            Logger.getLogger(PhieuThuePhong.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane2 = new javax.swing.JScrollPane();
        tbKhachDatPhong = new javax.swing.JTable();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbPhongDeThue = new javax.swing.JTable();
        jScrollPane3 = new javax.swing.JScrollPane();
        tbPhongThue = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        tfTenKhachHang = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        tfCMND = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        tfEmail = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        tfSoDienThoai = new javax.swing.JTextField();
        dpNgayThue = new org.jdesktop.swingx.JXDatePicker();
        jLabel6 = new javax.swing.JLabel();
        btLapPhieu = new javax.swing.JButton();
        btThoat = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        cbbSoKhach = new javax.swing.JComboBox();
        jLabel8 = new javax.swing.JLabel();
        dpNgayTra = new org.jdesktop.swingx.JXDatePicker();
        btLapMoi = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();
        tfTriGia = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jScrollPane2.setBorder(javax.swing.BorderFactory.createTitledBorder("Khách đã đặt phòng"));

        tbKhachDatPhong.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        tbKhachDatPhong.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbKhachDatPhongMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(tbKhachDatPhong);

        jScrollPane1.setBorder(javax.swing.BorderFactory.createTitledBorder("Thông tin phòng để thuê"));

        tbPhongDeThue.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        tbPhongDeThue.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbPhongDeThueMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tbPhongDeThue);

        jScrollPane3.setBorder(javax.swing.BorderFactory.createTitledBorder("Phòng thuê"));

        tbPhongThue.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, java.awt.Color.red));
        tbPhongThue.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        tbPhongThue.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbPhongThueMouseClicked(evt);
            }
        });
        jScrollPane3.setViewportView(tbPhongThue);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Thông tin khách"));

        jLabel2.setText("Tên Khách Hàng");

        jLabel5.setText("CMND");

        tfCMND.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                tfCMNDKeyTyped(evt);
            }
        });

        jLabel4.setText("Email");

        jLabel3.setText("Số điện thoại");

        dpNgayThue.setDate(new Date(System.currentTimeMillis()));
        dpNgayThue.setEditable(false);

        jLabel6.setText("Ngày Thuê");

        btLapPhieu.setText("Lập Phiếu");
        btLapPhieu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btLapPhieuActionPerformed(evt);
            }
        });

        btThoat.setText("Thoát");
        btThoat.setPreferredSize(new java.awt.Dimension(79, 23));
        btThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btThoatActionPerformed(evt);
            }
        });

        jLabel7.setText("Số khách");

        cbbSoKhach.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30" }));

        jLabel8.setText("Ngày Trả");

        dpNgayTra.setDate(new Date(System.currentTimeMillis()));

        btLapMoi.setText("Lập mới");
        btLapMoi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btLapMoiActionPerformed(evt);
            }
        });

        jLabel9.setText("Trị Giá");

        tfTriGia.setEditable(false);
        tfTriGia.setBackground(new java.awt.Color(204, 204, 204));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel6)
                            .addComponent(jLabel2)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel7)
                                .addComponent(jLabel8)
                                .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(34, 34, 34)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(dpNgayThue, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(tfTenKhachHang, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(cbbSoKhach, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(dpNgayTra, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(tfTriGia, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addGap(34, 34, 34)
                                .addComponent(tfEmail, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addGap(34, 34, 34)
                                .addComponent(tfCMND, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addGap(34, 34, 34)
                                .addComponent(tfSoDienThoai, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(22, 22, 22))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(btLapMoi)
                        .addGap(18, 18, 18)
                        .addComponent(btLapPhieu)
                        .addGap(18, 18, 18)
                        .addComponent(btThoat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btLapMoi, btLapPhieu, btThoat});

        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(tfTenKhachHang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(tfCMND, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(tfSoDienThoai, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(dpNgayThue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(dpNgayTra, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7)
                    .addComponent(cbbSoKhach, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(tfTriGia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btLapPhieu)
                    .addComponent(btThoat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btLapMoi))
                .addGap(13, 13, 13))
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {btLapMoi, btLapPhieu, btThoat});

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 412, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 410, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 502, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 228, Short.MAX_VALUE)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tfCMNDKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfCMNDKeyTyped
        // TODO add your handling code here:
        try {
            modelKhachDatPhong = KhachHang_BUS.laykhachDatPhong(tfCMND.getText());
        } catch (Exception ex) {
            Logger.getLogger(PhieuThuePhong.class.getName()).log(Level.SEVERE, null, ex);
        }
        tbKhachDatPhong.setModel(modelKhachDatPhong);

    }//GEN-LAST:event_tfCMNDKeyTyped

    private void tbPhongDeThueMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbPhongDeThueMouseClicked
        // TODO add your handling code here:
        int row = tbPhongDeThue.getSelectedRow();
        Vector rowData = new Vector();
        String maPhong = (String) this.modelPhongDeThue.getValueAt(row, 0).toString();
        String tenLoaiPhong = (String) this.modelPhongDeThue.getValueAt(row, 1).toString();
        String donGia = (String) this.modelPhongDeThue.getValueAt(row, 2).toString();
        rowData.add(maPhong);
        rowData.add(tenLoaiPhong);
        rowData.add(donGia);
        modelPhongThue.addRow(rowData);
        tbPhongThue.setModel(modelPhongThue);
        modelPhongDeThue.removeRow(row);
    }//GEN-LAST:event_tbPhongDeThueMouseClicked

    private void btThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btThoatActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btThoatActionPerformed

    private void btLapPhieuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btLapPhieuActionPerformed
        // TODO add your handling code here:
        //Kiểm tra ô nhập cmnd có được nhập không
        String CMND = tfCMND.getText();
        if (CMND.equals("")) {
            JOptionPane.showMessageDialog(rootPane, "Bạn phải nhập CMND!");
            return;
        }
        
        //kiểm tra khách hàng có phải đã đặt phòng không
        int rows = tbKhachDatPhong.getRowCount();

        int row = tbKhachDatPhong.getSelectedRow();
        if (rows != 0) {
            String maphieuthueMax;
            String maphieuThue;
            try {
                maphieuthueMax = PhieuThuePhong_BUS.laymaMax();
                if (maphieuthueMax.equals("")) {
                    maphieuThue = Phong_BUS.taoMaTuDong("MPT000000", 3);
                } else {
                    maphieuThue = Phong_BUS.taoMaTuDong(maphieuthueMax, 3);
                }
                String maKhach = tbKhachDatPhong.getValueAt(row, 0).toString();

                SimpleDateFormat format = new SimpleDateFormat("MM-dd-yyyy");
                Date dngayden = dpNgayThue.getDate();
                Date dngaytra = dpNgayTra.getDate();
                String ngaythue = format.format(dngayden);
                String ngaytra = format.format(dngaytra);
                String soKhach = cbbSoKhach.getSelectedItem().toString();

                if (dngaytra.before(dngayden)) {
                    JOptionPane.showMessageDialog(rootPane, "Ngày đi phải lớn hơn hoặc bằng ngày đến!");
                    return;
                }
                //Them CT PhieuThue

                String mactphieumax;
                String mactPhieu;


                int row_chon = tbPhongThue.getRowCount();
                if (row_chon <= 0) {
                    JOptionPane.showMessageDialog(rootPane, "Bạn phải chọn phòng cho khách!");
                    return;
                }

                PhieuThuePhong_DTO p = new PhieuThuePhong_DTO();
                p.setMaphieuThue(maphieuThue);
                p.setMaKhach(maKhach);
                p.setNgayThue(ngaythue);
                p.setNgayTra(ngaytra);
                p.setSoKhach(soKhach);
                p.setTriGia(tfTriGia.getText());
                KhachHang_BUS.capNhatTinhTrangKhach(maKhach, "Đang thuê");
                PhieuThuePhong_BUS.Insert(p);
                for (int i = 0; i < row_chon; i++) {
                    mactphieumax = CTPhieuThuePhong_BUS.laymaMax();
                    if (mactphieumax.equals("")) {
                        mactPhieu = Phong_BUS.taoMaTuDong("MCTPT000000", 5);
                    } else {
                        mactPhieu = Phong_BUS.taoMaTuDong(mactphieumax, 5);
                    }
                    String maPhong = tbPhongThue.getValueAt(i, 0).toString();
                    CTPhieuThuePhong_DTO ct = new CTPhieuThuePhong_DTO();
                    ct.setMactphieuThue(mactPhieu);
                    ct.setMaphieuThue(maphieuThue);
                    ct.setMaPhong(maPhong);
                    CTPhieuThuePhong_BUS.Insert(ct);
                    Phong_BUS.capnhattinhtrangDat(maPhong, "Đang thuê");
                }
                modelPhongThue.setNumRows(0);
                tfCMND.setText("");
                String maphieudat = PhieuDatPhong_BUS.layMaPhieuDat(maKhach);

                CTPhieuDatPhong_BUS.Delete(maphieudat);
                PhieuDatPhong_BUS.Delete(maphieudat);

                tbKhachDatPhong.setModel(modelKhachDatPhong);
                JOptionPane.showMessageDialog(rootPane, "Lập phiếu thành công!");
                new ShowReport().Show_ReportPhieuThue(maKhach);

            } catch (Exception ex) {
                Logger.getLogger(PhieuThuePhong.class.getName()).log(Level.SEVERE, null, ex);
            }

            //nếu khách hàng chưa đặt phòng mà còn phòng thì cho thuê mới
        } else {
            String makhach;
            String makhachmax;
            try {
                makhachmax = KhachHang_BUS.laymaMax();
                if (makhachmax.equals("")) {
                    makhach = Phong_BUS.taoMaTuDong("MK000000", 2);
                } else {
                    makhach = Phong_BUS.taoMaTuDong(makhachmax, 2);
                }
                String tenKhach = tfTenKhachHang.getText();
                String sdt = tfSoDienThoai.getText();
                String email = tfEmail.getText();
                String tinhtrang = "Đang thuê";



                String maphieuthueMax;
                String maphieuThue;

                maphieuthueMax = PhieuThuePhong_BUS.laymaMax();
                if (maphieuthueMax.equals("")) {
                    maphieuThue = Phong_BUS.taoMaTuDong("MPT000000", 3);
                } else {
                    maphieuThue = Phong_BUS.taoMaTuDong(maphieuthueMax, 3);
                }

                SimpleDateFormat format = new SimpleDateFormat("MM-dd-yyyy");
                Date dngayden = dpNgayThue.getDate();
                String ngaythue = format.format(dngayden);
                Date dngaytra = dpNgayTra.getDate();
                String ngaytra = format.format(dngaytra);
                String soKhach = cbbSoKhach.getSelectedItem().toString();

                if (dngaytra.before(dngayden)) {
                    JOptionPane.showMessageDialog(rootPane, "Ngày đi phải lớn hơn hoặc bằng ngày đến!");
                    return;
                }

                int row_chon = tbPhongThue.getRowCount();
                if (row_chon <= 0) {
                    JOptionPane.showMessageDialog(rootPane, "Bạn phải chọn phòng cho khách!");
                    return;
                }
                float dongia = 0;
                long songaythue = (dngaytra.getTime() - dngayden.getTime()) / 86400000 + 1;


                for (int i = 0; i < row_chon; i++) {
                    dongia += Float.parseFloat(tbPhongThue.getValueAt(i, 2).toString());
                }
                long trigia = (long) dongia * songaythue;
                tfTriGia.setText(Long.toString(trigia));

                KhachHang_DTO kh = new KhachHang_DTO();
                kh.setMaKhachHang(makhach);
                kh.setTenKhachHang(tenKhach);
                kh.setCmnd(CMND);
                kh.setDienThoai(sdt);
                kh.setEmail(email);
                kh.setTinhTrang(tinhtrang);
                KhachHang_BUS.Insert(kh);


                PhieuThuePhong_DTO p = new PhieuThuePhong_DTO();
                p.setMaphieuThue(maphieuThue);
                p.setMaKhach(makhach);
                p.setNgayThue(ngaythue);
                p.setNgayTra(ngaytra);
                p.setSoKhach(soKhach);
                p.setTriGia(tfTriGia.getText());
                PhieuThuePhong_BUS.Insert(p);



                String mactphieumax;
                String mactPhieu;
                for (int i = 0; i < row_chon; i++) {
                    mactphieumax = CTPhieuThuePhong_BUS.laymaMax();
                    if (mactphieumax.equals("")) {
                        mactPhieu = Phong_BUS.taoMaTuDong("MCTPT000000", 5);
                    } else {
                        mactPhieu = Phong_BUS.taoMaTuDong(mactphieumax, 5);
                    }
                    String maPhong = tbPhongThue.getValueAt(i, 0).toString();
                    CTPhieuThuePhong_DTO ct = new CTPhieuThuePhong_DTO();
                    ct.setMactphieuThue(mactPhieu);
                    ct.setMaphieuThue(maphieuThue);
                    ct.setMaPhong(maPhong);
                    CTPhieuThuePhong_BUS.Insert(ct);
                    Phong_BUS.capnhattinhtrangDat(maPhong, "Đang thuê");

                }
                JOptionPane.showMessageDialog(rootPane, "Lập phiếu thành công!");
                new ShowReport().Show_ReportPhieuThue(makhach);

            } catch (Exception ex) {
                Logger.getLogger(PhieuThuePhong.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
    }//GEN-LAST:event_btLapPhieuActionPerformed

    private void btLapMoiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btLapMoiActionPerformed
        // TODO add your handling code here:
        modelPhongThue.setNumRows(0);
        modelKhachDatPhong.setNumRows(0);
        tfTenKhachHang.setText("");
        tfCMND.setText("");
        tfSoDienThoai.setText("");
        tfEmail.setText("");
        tfTriGia.setText("");

    }//GEN-LAST:event_btLapMoiActionPerformed

    private void tbKhachDatPhongMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbKhachDatPhongMouseClicked
        // TODO add your handling code here:
        int row = tbKhachDatPhong.getSelectedRow();
        tfTenKhachHang.setText(tbKhachDatPhong.getValueAt(row, 1).toString());
        tfCMND.setText(tbKhachDatPhong.getValueAt(row, 2).toString());
        tfEmail.setText(tbKhachDatPhong.getValueAt(row, 3).toString());
        tfSoDienThoai.setText(tbKhachDatPhong.getValueAt(row, 4).toString());
        tfTriGia.setText(tbKhachDatPhong.getValueAt(row, 5).toString());
    }//GEN-LAST:event_tbKhachDatPhongMouseClicked

    private void tbPhongThueMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbPhongThueMouseClicked
        // TODO add your handling code here:
        int row = tbPhongThue.getSelectedRow();
        Vector rowData = new Vector();
        String maPhong = (String) this.modelPhongThue.getValueAt(row, 0).toString();
        String tenLoaiPhong = (String) this.modelPhongThue.getValueAt(row, 1).toString();
        String donGia = (String) this.modelPhongThue.getValueAt(row, 2).toString();
        rowData.add(maPhong);
        rowData.add(tenLoaiPhong);
        rowData.add(donGia);
        modelPhongDeThue.addRow(rowData);
        tbPhongDeThue.setModel(modelPhongDeThue);
        modelPhongThue.removeRow(row);
    }//GEN-LAST:event_tbPhongThueMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(PhieuThuePhong.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(PhieuThuePhong.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(PhieuThuePhong.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(PhieuThuePhong.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new PhieuThuePhong().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btLapMoi;
    private javax.swing.JButton btLapPhieu;
    private javax.swing.JButton btThoat;
    private javax.swing.JComboBox cbbSoKhach;
    private org.jdesktop.swingx.JXDatePicker dpNgayThue;
    private org.jdesktop.swingx.JXDatePicker dpNgayTra;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTable tbKhachDatPhong;
    private javax.swing.JTable tbPhongDeThue;
    private javax.swing.JTable tbPhongThue;
    private javax.swing.JTextField tfCMND;
    private javax.swing.JTextField tfEmail;
    private javax.swing.JTextField tfSoDienThoai;
    private javax.swing.JTextField tfTenKhachHang;
    private javax.swing.JTextField tfTriGia;
    // End of variables declaration//GEN-END:variables
}
